﻿using System;
using System.Collections.Generic;
using TMPro;
using UnityEngine;

// Token: 0x02000016 RID: 22
public class GruntText : MonoBehaviour
{
	// Token: 0x06000067 RID: 103 RVA: 0x0000CEB8 File Offset: 0x0000B0B8
	private void Start()
	{
		this.body = base.GetComponent<BodyScript>();
		this.ai = base.GetComponent<AI>();
		GameObject gameObject = Object.Instantiate<GameObject>(this.TextPrefab, this.body.head.transform.position, Quaternion.identity);
		this.text = gameObject.GetComponent<TextMeshPro>();
		this.text.text = "";
		this.curShowTime = -5f;
	}

	// Token: 0x06000068 RID: 104 RVA: 0x0000CF2C File Offset: 0x0000B12C
	private void Update()
	{
		float num = Mathf.Clamp(this.prevFrameHealth - this.body.health, 0f, 100f);
		this.text.transform.position = this.body.head.transform.position + Vector3.up * this.textHeight;
		this.curShowTime -= Time.deltaTime;
		if (this.curShowTime <= 0f)
		{
			this.text.text = "";
		}
		if (this.body.alive)
		{
			if (!this.ai && this.body.lastEngagedEnemy && !this.body.lastEngagedEnemy.alive)
			{
				this.body.lastEngagedEnemy = null;
				if (Random.Range(0f, 1f) < 0.75f)
				{
					this.Say(this.killLines[Random.Range(0, this.killLines.Count)], 3, 2f);
					if (this.body.voice)
					{
						this.body.voice.Cheer();
					}
				}
			}
			if (this.curShowTime < -2.5f)
			{
				if (!this.ai)
				{
					if (num > 10f)
					{
						this.Say(this.damageLines[Random.Range(0, this.damageLines.Count)], 1, 1.5f);
					}
				}
				else if (this.ai.target && !this.hadTargetPrev)
				{
					this.Say(this.alertLines[Random.Range(0, this.alertLines.Count)], 2, 2f);
				}
				else if (!this.ai.target && this.hadTargetPrev)
				{
					this.Say(this.killLines[Random.Range(0, this.killLines.Count)], 3, 2f);
				}
				else if (num > 10f)
				{
					this.Say(this.damageLines[Random.Range(0, this.damageLines.Count)], 1, 1.5f);
				}
			}
		}
		else if (!this.didDeathLine)
		{
			this.Say(this.deathLines[Random.Range(0, this.deathLines.Count)], 999, 2.5f);
			this.didDeathLine = true;
		}
		this.prevFrameHealth = this.body.health;
		if (this.ai)
		{
			this.hadTargetPrev = this.ai.target;
		}
	}

	// Token: 0x06000069 RID: 105 RVA: 0x0000D1F8 File Offset: 0x0000B3F8
	public void Say(string textt, int prior, float time)
	{
		if (Random.Range(0f, 1f) > 0.5f && (prior > this.curPriority || this.curShowTime < 0f))
		{
			this.text.text = textt;
			this.curShowTime = time;
			this.curPriority = prior;
		}
	}

	// Token: 0x0600006A RID: 106 RVA: 0x0000D24B File Offset: 0x0000B44B
	private void OnDestroy()
	{
		if (this.text)
		{
			Object.Destroy(this.text.gameObject);
			Object.Destroy(this);
		}
	}

	// Token: 0x0400012C RID: 300
	public List<string> alertLines;

	// Token: 0x0400012D RID: 301
	public List<string> killLines;

	// Token: 0x0400012E RID: 302
	public List<string> damageLines;

	// Token: 0x0400012F RID: 303
	public List<string> deathLines;

	// Token: 0x04000130 RID: 304
	private int curPriority;

	// Token: 0x04000131 RID: 305
	private float curShowTime;

	// Token: 0x04000132 RID: 306
	private TextMeshPro text;

	// Token: 0x04000133 RID: 307
	public float textHeight;

	// Token: 0x04000134 RID: 308
	public GameObject TextPrefab;

	// Token: 0x04000135 RID: 309
	private BodyScript body;

	// Token: 0x04000136 RID: 310
	private AI ai;

	// Token: 0x04000137 RID: 311
	private float talkTime = 5f;

	// Token: 0x04000138 RID: 312
	private float prevFrameHealth;

	// Token: 0x04000139 RID: 313
	private bool didDeathLine;

	// Token: 0x0400013A RID: 314
	private bool hadTargetPrev;
}
